import apiClient from "../apiClient";

export interface Project {
	id: number;
	name: string;
	url: string;
	createtime: number | null;
	updatetime: number | null;
	status: number;
	status_text: string;
}

export interface ProjectResponse {
	total: number;
	items: Project[];
}

export interface ApiResponse<T> {
	status: number;
	message: string;
	time: string;
	data: T;
}

export interface ProjectListParams {
	page: number;
	limit: number;
	keyword?: string;
}

export interface ProjectAddParams {
	name: string;
	url: string;
	status: number;
}

export interface ProjectEditParams {
	id: number;
	name?: string;
	url?: string;
	status?: number;
}

export enum ProjectApi {
	ProjectList = "/project/index",
	ProjectAdd = "/project/add",
	ProjectEdit = "/project/edit",
	ProjectDelete = "/project/delete",
	ProjectInfo = "/project/info",
}

const projectList = (params: ProjectListParams) =>
	apiClient.get<ApiResponse<ProjectResponse>>({
		url: ProjectApi.ProjectList,
		params,
	});

const projectAdd = (data: ProjectAddParams) =>
	apiClient.post<ApiResponse<Project>>({ url: ProjectApi.ProjectAdd, data });

const projectEdit = (data: ProjectEditParams) =>
	apiClient.put<ApiResponse<Project>>({ url: ProjectApi.ProjectEdit, data });

const projectDelete = (id: number) =>
	apiClient.delete<ApiResponse<null>>({
		url: ProjectApi.ProjectDelete,
		data: { id },
	});

const projectInfo = (id: number) =>
	apiClient.get<ApiResponse<Project>>({
		url: `${ProjectApi.ProjectInfo}/${id}`,
	});

export const projectService = {
	projectList,
	projectAdd,
	projectEdit,
	projectDelete,
	projectInfo,
};
